package com.erification.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.erification.web.model.Address;
import com.erification.web.model.Celephone;
import com.erification.web.model.Email;
import com.erification.web.model.Group;
import com.erification.web.model.TrueName;
import com.erification.web.model.User;
import com.erification.web.service.AddressService;
import com.erification.web.service.CelephoneService;
import com.erification.web.service.EmailService;
import com.erification.web.service.GroupService;
import com.erification.web.service.TrueNameService;
import com.erification.web.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@Controller("addAction")
public class AddAction extends ActionSupport {
	@Resource 
	private UserService userService;
	@Resource
	private AddressService addressService;
	@Resource
	private CelephoneService celephoneService;
	@Resource
	private EmailService emailService;
	@Resource
	private TrueNameService trueNameService;
	@Resource
	private GroupService groupService;
	
	private TrueName trueName;
	private Address address;
	private Email email;
	private Celephone celephone;
	
	private String groupName;
	private String nameId;
	private String addressId;
	private String emailId;
	private String celephoneId;
	
	public TrueName getTrueName() {
		return trueName;
	}
	public void setTrueName(TrueName trueName) {
		this.trueName = trueName;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	public Email getEmail() {
		return email;
	}
	public void setEmail(Email email) {
		this.email = email;
	}
	public Celephone getCelephone() {
		return celephone;
	}
	public void setCelephone(Celephone celephone) {
		this.celephone = celephone;
	}
	public String getGroupName() {
		return groupName;
	}
	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}
	public String getNameId() {
		return nameId;
	}
	public void setNameId(String nameId) {
		this.nameId = nameId;
	}
	public String getAddressId() {
		return addressId;
	}
	public void setAddressId(String addressId) {
		this.addressId = addressId;
	}
	public String getEmailId() {
		return emailId;
	}
	public void setEmailId(String emailId) {
		this.emailId = emailId;
	}
	public String getCelephoneId() {
		return celephoneId;
	}
	public void setCelephoneId(String celephoneId) {
		this.celephoneId = celephoneId;
	}
	public String addTrueName() {
		String username = (String) ActionContext.getContext().getSession().get("username");
		User user = userService.getUser(username);
		trueName.setUser(user);
		trueNameService.saveTrueName(trueName);
		List list = (List) ActionContext.getContext().getSession().get("truename");
		list.add(trueName);
		ActionContext.getContext().getSession().put("truename", list);
		return SUCCESS;
	}
	public String addAddress() {
		String username = (String) ActionContext.getContext().getSession().get("username");
		User user = userService.getUser(username);
		address.setUser(user);
		addressService.saveAddress(address);
		List list = (List) ActionContext.getContext().getSession().get("address");
		list.add(address);
		ActionContext.getContext().getSession().put("address", list);
		return SUCCESS;
	}
	public String addEmail() {
		String username = (String) ActionContext.getContext().getSession().get("username");
		User user = userService.getUser(username);
		email.setUser(user);
		emailService.saveEmail(email);
		List list = (List) ActionContext.getContext().getSession().get("email");
		list.add(email);
		ActionContext.getContext().getSession().put("email", list);
		return SUCCESS;
	}
	public String addCelephone() {
		String username = (String) ActionContext.getContext().getSession().get("username");
		User user = userService.getUser(username);
		celephone.setUser(user);
		celephoneService.saveCelephone(celephone);
		List list = (List) ActionContext.getContext().getSession().get("celephone");
		list.add(celephone);
		ActionContext.getContext().getSession().put("celephone", list);
		return SUCCESS;
	}
	public String addGroup(){
		String username =  (String) ActionContext.getContext().getSession().get("username");
		User user = userService.getUser(username);
		Group group = new Group();
		group.setGroupName(groupName);
		group.setUser(user);
		group.setCelephone(celephoneService.get(Long.parseLong(celephoneId)));
		group.setAddress(addressService.get(Long.parseLong(addressId)));
		group.setTrueName(trueNameService.get(Long.parseLong(nameId)));
		group.setEmail(emailService.get(Long.parseLong(emailId)));
		groupService.save(group);
		List<Group> list = (List)ActionContext.getContext().getSession().get("group");
		list.add(group);
		ActionContext.getContext().getSession().put("group", list);
		return "group";
	}
}
